package com.ruoyi.web.controller.glue;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GlueProduct;
import com.ruoyi.system.domain.dto.AdhesiveModelDto;
import com.ruoyi.system.domain.vo.AdhesiveModelVo;
import com.ruoyi.system.service.IGlueProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 胶粘产品
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/system/product")
public class GlueProductController extends BaseController
{
    @Autowired
    private IGlueProductService glueProductService;

    /**
     * 查询胶粘产品列表
     * @return {@link GlueProduct}
     */
    //@PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(GlueProduct glueProduct)
    {
        startPage();
        List<GlueProduct> list = glueProductService.selectGlueProductList(glueProduct);
        return getDataTable(list);
    }

    /**
     * 导出胶粘产品列表
     */
    //@PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "胶粘产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GlueProduct glueProduct)
    {
        List<GlueProduct> list = glueProductService.selectGlueProductList(glueProduct);
        ExcelUtil<GlueProduct> util = new ExcelUtil<GlueProduct>(GlueProduct.class);
        util.exportExcel(response, list, "胶粘产品数据");
    }

    /**
     * 导入胶粘产品
     */
    //@PreAuthorize("@ss.hasPermi('system:product:import')")
    @Log(title = "胶粘产品", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importGlueProduct(@RequestBody MultipartFile file)
    {
        ExcelUtil<GlueProduct> util = new ExcelUtil<GlueProduct>(GlueProduct.class);
        try {
            Date nowDate = DateUtils.getNowDate();
            List<GlueProduct> glueProducts = util.importExcel(file.getInputStream());
            if (!CollectionUtils.isEmpty(glueProducts)){
                glueProducts=glueProducts.stream().filter(item->!ObjectUtils.isEmpty(item)).collect(Collectors.toList());
            }
            glueProducts.forEach(item->{
                item.setCreateBy(getUserId().toString());
                item.setCreateTime(nowDate);
            });
            if (CollectionUtils.isEmpty(glueProducts)){
                throw new RuntimeException("没有读取到表格数据，请检查表格表头是否与提供的模板表头一致。");
            }
            glueProductService.saveBatch(glueProducts);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.success(e.getMessage());
        }
        return AjaxResult.success();
    }


    /**
     * 下载胶粘产品导入模板
     */
    //@PreAuthorize("@ss.hasPermi('system:product:import')")
    @Log(title = "胶粘产品", businessType = BusinessType.IMPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<GlueProduct> util = new ExcelUtil<GlueProduct>(GlueProduct.class);
        util.exportExcel(response, null, "胶粘产品导入模板");
    }

    /**
     * 获取胶粘产品详细信息
     * @return {@link GlueProduct}
     */
    //@PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(glueProductService.selectGlueProductById(id));
    }

    /**
     * 新增胶粘产品
     */
    //@PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "胶粘产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GlueProduct glueProduct)
    {
        return toAjax(glueProductService.insertGlueProduct(glueProduct));
    }

    /**
     * 修改胶粘产品
     */
    //@PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "胶粘产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GlueProduct glueProduct)
    {
        return toAjax(glueProductService.updateGlueProduct(glueProduct));
    }

    /**
     * 删除胶粘产品
     */
    //@PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "胶粘产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(glueProductService.deleteGlueProductByIds(ids));
    }


    /**
     * 获取选型三级目录
     * @param dto
     * @return {@link AdhesiveModelVo}
     */
   @GetMapping("/getModel")
    public AjaxResult getModel(AdhesiveModelDto dto){
       AdhesiveModelVo adhesiveModelVo =   glueProductService.getModel(dto);
      return AjaxResult.success(adhesiveModelVo);
   }

}
